<template>
    <div class="org-tree-wrap">
        <vue2-org-tree
            :data="data"
            :props="props"
            :label-width="150"
            :collapsable="collapsable"
            :expandAll="expandAll"
            :label-class-name="labelClassName"
            @on-expand="onExpand">
            <!-- :render-content="renderContent" -->
        </vue2-org-tree>
    </div>
</template>
<script>
import Vue2OrgTree from 'vue2-org-tree'
import { getDepartmentTreeAndAllUser } from '@/api/department'

export default {
    name: 'OrgTree',
    props: {
        orgId: {
            required: true,
        },
        orgIds: {
            required: true,
        }
    },
    components: {
        Vue2OrgTree
    },
    data() {
        return {
            data: {},
            props: {
                label: 'name',
                expand: 'expand',
                children: 'nodes'
            },
            collapsable: true, // 是否可收缩
            expandAll: true, // 是否展开全部
        }
    },
    created() {
        this.getData()
    },
    methods: {
        getData() {
            let that = this
            getDepartmentTreeAndAllUser({
                orgId: that.orgId,
                orgIds: that.orgIds,
            }).then(res => {
                if(res.status === 200) {
                    that.data = res.data[0]
                    // 默认展开全部节点
                    that.toggleExpand(res.data[0], that.expandAll)
                }
            })
        },
        labelClassName(data) {
            if(data.isDepart === 0) {
                return 'bg-blue';
            } else {
                return 'bg-yellow';
            }
        },
        renderContent(h, data) {
            // if(data.isDepart === 0) {
            //     return (<p class=""><i class="iconfont icon-bumen" style="margin-right: 5px;"></i>{data.name}</p>);
            // } else {
            //     return (<p><i class="iconfont icon-renyuan" style="margin-right: 5px;"></i>{data.name}</p>);
            // }
        },
        onExpand(e, data) {
            if ("expand" in data) {
                data.expand = !data.expand;
                if (!data.expand && data.nodes) {
                    this.collapse(data.nodes);
                }
            } else {
                this.$set(data, "expand", true);
            }
        },
        // onNodeClick(e, data) {
            // 看是节点还是按钮
            // var target=e.target;
            // var classList=[...e.target.classList];
            // var label=classList.find(item=> item=="org-tree-node-label-inner")
            // if(label){
            //     this.$store.commit("setCurrentNode",data)
            // }else{
            //     //alert(data.name)
            // }
        // },
        collapse(list) {
            var _this = this;
            list.forEach(function(child) {
                if (child.expand) {
                    child.expand = false;
                }
                child.nodes && _this.collapse(child.nodes);
            });
        },
        toggleExpand(data, val) {
            var _this = this;
            if (Array.isArray(data)) {
                data.forEach(function(item) {
                    _this.$set(item, "expand", val);
                    if (item.nodes) {
                        _this.toggleExpand(item.nodes, val);
                    }
                });
            } else {
                this.$set(data, "expand", val);
                if (data.nodes) {
                    _this.toggleExpand(data.nodes, val);
                }
            }
        }
    }
}
</script>
<style lang="scss">
.org-tree-wrap {
    display: flex;
    overflow: auto;
    width: 100%;
    min-height: 700px;
    .bg-blue {
        color: #FFF;
        background-color: #4CA4FE;
        .org-tree-node-btn{
            font-size: 20px;
        }
    }
    .bg-yellow {
        color: #FFF;
        background-color: #FFB32D;
        .org-tree-node-btn{
            font-size: 20px;
        }
    }
}
// 树样式
.org-tree-container {
  display: inline-block;
  padding: 15px;
  overflow: auto;
  // margin: 0 auto;
  
  position: absolute;
  // height: 500px;
    top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  margin-top: 50px;
}

.org-tree {
  // display: inline-block;
  display: table;
  text-align: center;

  &:before, &:after {
    content: '';
    display: table;
  }

  &:after {
    clear: both;
  }
}

.org-tree-node,
.org-tree-node-children {
  position: relative;
  margin: 0;
  padding: 0;
  list-style-type: none;

  &:before, &:after {
    transition: all .5s;
  }
}
.org-tree-node-label {
  position: relative;
  display: inline-block;

  .org-tree-node-label-inner {
    cursor: pointer;
    padding: 6px 0px;
    text-align: center;
    border-radius: 3px;
    box-shadow: 0 1px 5px rgba(0, 0, 0, .7);
    font-size: 16px;
  }
}
.org-tree-node-btn {
  position: absolute;
  top: 100%;
  left: 50%;
  width: 20px;
  height: 20px;
  z-index: 10;
  margin-left: -11px;
  margin-top: 9px;
  background-color: #fff;
  border: 1px solid #d2d2d2;
  border-radius: 50%;
  box-shadow: 0 0 2px rgba(0, 0, 0, .15);
  cursor: pointer;
  transition: all .35s ease;

  &:hover {
    background-color: #e7e8e9;
    transform: scale(1.15);
  }

  &:before, &:after {
    content: '';
    position: absolute;
  }

  &:before {
    top: 50%;
    left: 4px;
    right: 4px;
    height: 0;
    border-top: 1px solid #d2d2d2;
  }

  &:after {
    top: 4px;
    left: 50%;
    bottom: 4px;
    width: 0;
    border-left: 1px solid #d2d2d2;
  }

  &.expanded:after {
    border: none;
  }
}
.org-tree-node {
  padding-top: 50px;
  display: table-cell;
  vertical-align: top;
  position: relative;

  &.is-leaf, &.collapsed {
    padding-left: 10px;
    padding-right: 10px;
  }

  &:before, &:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 50px;
  }

  &:after {
    left: 50%;
    border-left: 1px solid #d2d2d2;
    // background: url('../image/jt.png') 0 0 no-repeat;
    // background-size: 100% 100%;
    // background-position-x: -25px;

  }

  &:not(:first-child):before,
  &:not(:last-child):after {
    border-top: 1px solid #d2d2d2;
  }

}
.collapsable .org-tree-node.collapsed {
  padding-bottom: 30px;

  .org-tree-node-label:after {
    content: '';
    position: absolute;
    top: 100%;
    left: 0;
    width: 50%;
    height: 20px;
    border-right: 1px solid #d2d2d2;
  }
}
.org-tree > .org-tree-node {
  padding-top: 0;

  &:after {
    border-left: 0;
  }
}
.org-tree-node-children {
  padding-top: 50px;
  display: table;

  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    height: 50px;
    border-right: 1px solid #d2d2d2;
    border-left: none;
  }

  &:after {
    content: '';
    display: table;
    clear: both;
  }
}

.horizontal {
  .org-tree-node {
    // display: flex;
    // flex-direction: row;
    // justify-content: flex-start;
    // align-items: center;
    display: table-cell;
    float: none;
    padding-top: 0;
    padding-left: 20px;

    &.is-leaf, &.collapsed {
      padding-top: 10px;
      padding-bottom: 10px;
    }

    &:before, &:after {
      width: 19px;
      height: 50%;
    }

    &:after {
      top: 50%;
      left: 0;
      border-left: 0;
    }

    &:only-child:before {
      top: 1px;
      border-bottom: 1px solid #d2d2d2;
    }

    &:not(:first-child):before,
    &:not(:last-child):after {
      border-top: 0;
      border-left: 1px solid #d2d2d2;
    }

    &:not(:only-child):after {
      border-top: 1px solid #d2d2d2;
    }

    .org-tree-node-inner {
      display: table;
    }

  }

  .org-tree-node-label {
    display: table-cell;
    vertical-align: middle;
  }

  &.collapsable .org-tree-node.collapsed {
    padding-right: 30px;

    .org-tree-node-label:after {
      top: 0;
      left: 100%;
      width: 20px;
      height: 50%;
      border-right: 0;
      border-bottom: 1px solid #d2d2d2;
    }
  }

  .org-tree-node-btn {
    top: 50%;
    left: 100%;
    margin-top: -11px;
    margin-left: 9px;
  }

  & > .org-tree-node:only-child:before {
    border-bottom: 0;
  }

  .org-tree-node-children {
    // display: flex;
    // flex-direction: column;
    // justify-content: center;
    // align-items: flex-start;
    display: table-cell;
    padding-top: 0;
    padding-left: 20px;

    &:before {
      top: 50%;
      left: 0;
      width: 20px;
      height: 0;
      border-left: 0;
      border-top: 1px solid #d2d2d2;
    }

    &:after {
      display: none;
    }

    & > .org-tree-node {
      display: block;
    }
  }
}
</style>
